package top.alanshelby.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import top.alanshelby.entity.Item;
import top.alanshelby.service.ItemService;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping(value = "upload")
public class FileUploadController {

    private static final String BASEDIR = "E:\\Project\\CourseProject\\Course-Chapter\\Chapter28\\fileUpload\\";

    @Autowired
    private ItemService itemService;

    @RequestMapping(value = "showItem")
    public String showItem(Integer id, Model model) {
        Item item = itemService.getItemById(id);
        model.addAttribute("item", item);
        return "itemEdit";
    }

    @RequestMapping(value = "showItem1")
    public String showItem1(Integer id, Model model) {
        Item item = itemService.getItemById(id);
        model.addAttribute("item", item);
        return "itemEdit1";
    }

    @RequestMapping(value = "singleFileUpload")
    @ResponseBody
    public String singleFileUpload(Item item, MultipartFile pictureFile) throws IOException {
        if (pictureFile != null) {
            String newFileName = saveFile(pictureFile);
            item.setPic(newFileName);
        }
        // 商品修改
        itemService.updateItem(item);
        return "success";
    }

    @RequestMapping(value = "multipartFileUpload")
    @ResponseBody
    public String multipartFileUpload(Item item, MultipartFile[] pictureFiles) throws IOException {
        //判断pictureFiles数组不能为空并且长度大于0
        if (pictureFiles != null && pictureFiles.length > 0) {
            //循环获取file数组中得文件
            for (int i = 0; i < pictureFiles.length; i++) {
                MultipartFile pictureFile = pictureFiles[i];
                //保存文件
                String newFileName = saveFile(pictureFile);
                if (i == 0) {
                    item.setPic(newFileName);
                } else if (i == 1) {
                    item.setPic1(newFileName);
                }
            }
            // 商品修改
            itemService.updateItem(item);
        }
        return "success";
    }

    public String saveFile(MultipartFile pictureFile) throws IOException {
        String newFileName = null;
        if (pictureFile != null) {
            // 获取文件名称
            String originalFilename = pictureFile.getOriginalFilename();
            if (originalFilename != null && !"".contentEquals(originalFilename)) {
                // 获取扩展名
                String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
                // 重新生成一个新的文件名
                newFileName = UUID.randomUUID().toString() + extName;
                // 指定存储文件的根目录
                File dirFile = new File(BASEDIR);
                if (!dirFile.exists()) {
                    dirFile.mkdirs();
                }
                // 将上传的文件复制到新的文件（完整路径）中
                pictureFile.transferTo(new File(BASEDIR + newFileName));
            }
        }
        return newFileName;
    }
}
